﻿using StudyEnglish.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace StudyEnglish.ViewModel
{
    public class UserViewModel : INotifyPropertyChanged
    {
        private AppDataContext appDB;

        private ObservableCollection<AppUser> _lstUser;
        public ObservableCollection<AppUser> LstUser//getUserData
        {
            get { return _lstUser; }
            set
            {
                _lstUser = value;
                NotifyPropertyChanged("LstUser");
            }
        }

        public UserViewModel(string appConnectionString)
        {
            appDB = new AppDataContext(appConnectionString);
        }

        public void saveChangeDB()
        {
            appDB.SubmitChanges();
        }

        public void AddUser(AppUser newUser)
        {
            appDB.User.InsertOnSubmit(newUser);
            appDB.SubmitChanges();

        }
        

        public void getUser(string use, string pass)
        {
           var lst = (from s in appDB.User 
                           where s.username == use && s.password == pass
                            select s);
           _lstUser = new ObservableCollection<AppUser>(lst);
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

    }
}
